package com.uplooking.util;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.uplooking.pojo.UserVO;

public class PasswordUtils {

	private static SecureRandomNumberGenerator generator = null;

	static {
		generator = new SecureRandomNumberGenerator();
	}

	public static void encrypt(UserVO record) {
		String algorithmName = "MD5";
		int hashIterations = 1;
		String source = record.getUrpwd();
		if (source==null) {
			source = "123456";
		}
		record.setUrsalt(generator.nextBytes().toHex());
		record.setUrpwd(new SimpleHash(algorithmName, source, ByteSource.Util.bytes(record.getUrsalt()), hashIterations)
				.toHex());
	}

	public static Boolean ckpwd(UserVO userVO, UserVO record) {
		String algorithmName = "MD5";
		int hashIterations = 1;
		String source = userVO.getUrpwd();
		userVO.setUrpwd(new SimpleHash(algorithmName, source, ByteSource.Util.bytes(record.getUrsalt()), hashIterations)
				.toHex());
		return userVO.getUrpwd().equals(record.getUrpwd());
	}
}
